package com.jasic.test.socketServer;

import com.jasic.comnt.protocol.bean.LoginResp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * User: Jasic
 * Date: 13-1-4
 */
public class SimpleSocketServer {


    private ServerSocket server;

    private int port;

    private Map<Socket, SocketThread> socketThreadMap;

    public SimpleSocketServer(int port) {
        this.port = port;
        try {
            this.server = new ServerSocket(port);
            this.socketThreadMap = new ConcurrentHashMap<Socket, SocketThread>();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void recv() {
        try {
            while (true){
            Socket socket = this.server.accept();
            SocketThread thread = this.socketThreadMap.get(socket);
            if (null == thread) {
                thread = new SocketThread(socket);
                socketThreadMap.put(socket, thread);
                thread.start();
            }
        }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    class SocketThread extends Thread {

        Socket socket;

        SocketThread(Socket socket) {
            this.socket = socket;
        }


        @Override
        public void run() {

                recv();

        }


        private void recv() {
                int index = 0;
            try {
                InputStream sis = this.socket.getInputStream();
                byte[] b = new byte[1024];
                while (true) {
                    index++;
                    int len = sis.read(b);
                    if (len == -1) {
                        System.out.println("read code:-1, return!");
                        return;
                    }

                    System.out.print("Receive loginresp....: ");
                    for (int i = 0; i < len; i++) {
                        System.out.print(b[i] + " ");
                    }
                    System.out.println();
                    System.out.print("Sending loginresp....");

                    OutputStream sos = this.socket.getOutputStream();


                    LoginResp resp = new LoginResp(0);
                    byte[] bys = resp.toByte();
                    for (byte bs : bys){
                        System.out.print(bs + " ");
                    }

                    System.out.println();

                    sos.write(bys);
                    sos.flush();

                    System.out.println("接收与发送与成功");


                System.out.print(index);
                }
            } catch (IOException e) {
                e.printStackTrace();
                System.out.print(index);
            }
        }
    }

    public static void main(String[] args) {
        new SimpleSocketServer(4444).recv();
    }
}
